package com.cction.fragment

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.cction.R
import com.cction.activity.reinterest.CompoundListActivity
import com.cction.activity.house.HouseListActivity
import com.cction.activity.service.ServiceActivity
import com.cction.comm.LinDividerItemDecoration
import com.cction.comm.SaveCompoundUtils
import com.cction.comm.SaveHouseUtils
import com.cction.entry.UserItemBean
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.user_main.*

/**
 * 用户页面
 */
class UserFragment : Fragment() {
    private val arrayList = ArrayList<UserItemBean>()
    private lateinit var adapter: CommonAdapter<UserItemBean>

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.user_main, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        initView()

    }

    private fun initView() {


        user_recyler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        user_recyler.addItemDecoration(LinDividerItemDecoration(context))

        adapter = object : CommonAdapter<UserItemBean>(context, R.layout.user_item, arrayList) {
            override fun convert(holder: ViewHolder, t: UserItemBean, position: Int) {
                holder.setVisible(R.id.user_item_spacing, t.isSpacing)
                holder.setText(R.id.user_item_name, t.name)
                if (TextUtils.isEmpty(t.other) || t.other == "0") {
                    holder.setText(R.id.user_item_other, "")
                } else {
                    holder.setText(R.id.user_item_other, t.other)
                }
                holder.setImageResource(R.id.user_item_image, t.icon)
                holder.itemView.setOnClickListener {
                    when (t.name) {
                        "计算收藏" -> {
                            startActivity(Intent(context, CompoundListActivity::class.java))
                        }
                        "房贷收藏" -> {
                            startActivity(Intent(context, HouseListActivity::class.java))
                        }
                    }
                }
            }
        }
        user_recyler.adapter = adapter


        user_service.setOnClickListener { toService() }
    }


    private fun toService() {
        startActivity(Intent(context, ServiceActivity::class.java))
    }

    override fun onResume() {
        super.onResume()

        arrayList.clear()
        val compounds = SaveCompoundUtils.readData(context)
        val houses = SaveHouseUtils.readData(context)
        val compoundSize = compounds.arrayList.size
        val houseSize = houses.arrayList.size
        arrayList.add(UserItemBean(false, "计算收藏", R.drawable.svg_collection, "$compoundSize"))
        arrayList.add(UserItemBean(false, "房贷收藏", R.drawable.svg_money, "$houseSize"))
//        arrayList.add(UserItemBean(true, "我要说", R.drawable.svg_feedback, ""))

        adapter.notifyDataSetChanged()
    }
}

